//
//  HomeStrategyDetailController.m
//  GiftNote
//
//  Created by lanou on 15/8/17.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#define kStrategyDetailHead @"http://api.liwushuo.com/v2/brands/"
#define kStrategyDetail @"/items?limit=20&offset="

#import "HomeStrategyDetailController.h"
#import "HomeHeader.h"
#import "HomeStrategyDetailCell.h"
#import "LORequestManger.h"
#import "HomeStrategyDetailModel.h"
#import "HomeDetailBuyViewController.h"

@interface HomeStrategyDetailController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger num;

@end


@implementation HomeStrategyDetailController

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _num = 0;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self readCellData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 创建collectionView

- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((kHomeWidth - 30) / 2, (kHomeWidth - 30) / 2 + 95);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[HomeStrategyDetailCell class] forCellWithReuseIdentifier:@"HomeStrategyDetailCell"];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeStrategyDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeStrategyDetailCell" forIndexPath:indexPath];
    cell.detailModel = _dataSource[indexPath.item];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(30, 10, 30, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeDetailBuyViewController *detailBuyVC = [[HomeDetailBuyViewController alloc]init];
    HomeStrategyDetailModel *model = _dataSource[indexPath.item];
    detailBuyVC.ID = [NSString stringWithFormat:@"%ld",(long)[model.ID intValue]];
    [self.navigationController pushViewController:detailBuyVC animated:YES];
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

- (void)readCellData
{
    NSString *url = [kStrategyDetailHead stringByAppendingString:_ID];
    NSString *newUrl = [[url stringByAppendingString:kStrategyDetail] stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)_num]];
    [LORequestManger GET:newUrl success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *array = dic[@"data"][@"items"];
        for (NSDictionary *newDic in array) {
            HomeStrategyDetailModel *detailModel = [[HomeStrategyDetailModel alloc]init];
            [detailModel setValuesForKeysWithDictionary:newDic];
            [self.dataSource addObject:detailModel];
        }
        
        [self creatCollectionView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
